package org.ponly.cta4j.drawing.text.glyphsvisitor;

import org.ponly.cta4j.drawing.text.Glyphs;

import java.awt.geom.Rectangle2D;

/**
 * 文字重叠.
 *
 * @author mag
 * @Date 6 mars 2008
 */
public class OverlapGlyphsVisitor implements GlyphsVisitor {

    private double overlapPixels = 0;

    public OverlapGlyphsVisitor(double overlapPixels) {
        this.overlapPixels = overlapPixels;
    }

    public void visit(Glyphs glyphs, Rectangle2D backgroundBounds) {
        for (int i = 1; i < glyphs.size(); i++) {
            /*-
             *  ------   -----
             * |  文  | |  字  |
             *  ------   -----
             * 上一个右跨距 + 间距 + 下一个左跨距
             */
            double spacing = glyphs.getBoundsX(i) - glyphs.getBoundsX(i - 1) - glyphs.getBoundsWidth(i - 1);
            double tx = -(spacing + Math.abs(glyphs.getRSB(i - 1)) + Math.abs(glyphs.getLSB(i)) + overlapPixels);
            double ty = 0;

            glyphs.translate(i, tx, ty);
        }
    }


}